import json
import csv
from config import EXPORT_JSON, EXPORT_CSV
from logger import get_logger

logger = get_logger('Exporter')

def export_data(detections):
    try:
        with open(EXPORT_JSON, 'w') as jf:
            json.dump(detections, jf, indent=2)
        with open(EXPORT_CSV, 'w', newline='') as cf:
            writer = csv.DictWriter(cf, fieldnames=detections[0].keys())
            writer.writeheader()
            writer.writerows(detections)
        logger.info("Exported detection data to JSON and CSV.")
    except Exception as e:
        logger.error(f"Export failed: {e}")
